package com.br.view;

import java.util.ArrayList;
import java.util.List;

import com.br.dao.ColigadoDao;
import com.br.dao.EmpresaDao;
import com.br.model.Coligado;
import com.br.model.Empresa;

public class ColigadoView {
	
private String geraAlterar(String pColigada, String pEmpresa){
		
		//Objetos
		EmpresaDao  empresaDao  = new EmpresaDao();
		Empresa     empresa     = new Empresa();
		Coligado    coligada    = new Coligado();
		ColigadoDao coligadaDao = new ColigadoDao();
		
		//variaveis
		String vCodeLine             = null;
		List<String>   vCampos       = new ArrayList<String>();
		List<String>   vValores		 = new ArrayList<String>();
		List<Empresa>  listaEmpresa  = new ArrayList<Empresa>();
		List<Coligado> listaColigado = new ArrayList<Coligado>();
		
		vCampos.add("COD_COLIGADA");
		vCampos.add("COD_EMPRESA");
		
		vValores.add(pColigada);
		vValores.add(pEmpresa);
		
		listaColigado = coligadaDao.consulta(vCampos, vValores);
		
		coligada = listaColigado.get(0);
		
		listaEmpresa = empresaDao.consulta(null, null);		
		
		//Monta Codigo
		vCodeLine  = "<div id=\"inclusaoMeioRodape\">";
		vCodeLine += "</br>";
		vCodeLine += "</br>";
		vCodeLine += " <form action=\"cm.jsp\" method=\"post\">";
		vCodeLine += "  <p align=\"center\"><strong>Cadastro de Coligado</strong></p>";
		vCodeLine += "  </br>";
		vCodeLine += "  <p><strong>&nbsp;&nbsp; Coligada &nbsp;&nbsp;<input name=\"txtColigada\" type=\"text\" size=\"32\" value="+coligada.getNOM_COLIGADA()+"\"></strong></p>";
		vCodeLine += "  <p><strong>&nbsp;&nbsp;  Empresa  &nbsp; <select style=width:306px name=\"txtEmpresa\">";
		vCodeLine += " <option value=\"null\" size=\"45\"></option>";
		for (int i = 0; i < listaEmpresa.size(); i++){
			empresa = listaEmpresa.get(i);
			
			if (empresa.getCOD_EMPRESA() == Integer.parseInt(coligada.getCOD_EMPRESA())){
				vCodeLine += " <option value=\""+empresa.getCOD_EMPRESA()+"\" size=\"45\" selected>"+empresa.getNOM_EMPRESA()+"</option>";
			}else{
				vCodeLine += " <option value=\""+empresa.getCOD_EMPRESA()+"\" size=\"45\">"+empresa.getNOM_EMPRESA()+"</option>";
			}	
		}
		vCodeLine += "  </select></strong></p>";
		vCodeLine += " <input name=\"txtCodColigada\" type=\"text\" size=\"32\" value =\""+coligada.getCOD_COLIGADA()+"\" style=\"visibility:hidden\">";
		vCodeLine += "  <p align=\"center\"><input type=\"submit\" value=\"    Salvar    \"></p>";   
		vCodeLine += " </form>";
		vCodeLine += "</br>";
		vCodeLine += "</div>";
		
		return vCodeLine; 
	}	
	
	private String geraCadastro(){
		
		//Objetos
		EmpresaDao empresaDao = new EmpresaDao();
		Empresa    empresa    = new Empresa();
		
		//variaveis
		String vCodeLine       = null;
		List<Empresa> listaEmpresa = new ArrayList<Empresa>();
		
		listaEmpresa = empresaDao.consulta(null, null);
		
		//Monta Codigo
		vCodeLine  = "<div id=\"inclusaoMeioRodape\">";
		vCodeLine += "</br>";
		vCodeLine += "</br>";
		vCodeLine += " <form action=\"cm.jsp\" method=\"post\">";
		vCodeLine += "  <p align=\"center\"><strong>Cadastro de Coligado</strong></p>";
		vCodeLine += "  </br>";
		vCodeLine += "  <p><strong>&nbsp;&nbsp; Coligada &nbsp;&nbsp;<input name=\"txtColigada\" type=\"text\" size=\"32\"></strong></p>";
		vCodeLine += "  <p><strong>&nbsp;&nbsp;  Empresa  &nbsp; <select style=width:306px name=\"txtEmpresa\">";
		vCodeLine += " <option value=\"null\" size=\"45\"></option>";
		for (int i = 0; i < listaEmpresa.size(); i++){
			empresa = listaEmpresa.get(i);
			vCodeLine += " <option value=\""+empresa.getCOD_EMPRESA()+"\" size=\"45\">"+empresa.getNOM_EMPRESA()+"</option>";
		}
		vCodeLine += "  </select></strong></p>";
		vCodeLine += "  <p align=\"center\"><input type=\"submit\" value=\"    Salvar    \"></p>";   
		vCodeLine += " </form>";
		vCodeLine += "</br>";
		vCodeLine += "</div>";
		
		return vCodeLine; 
	}
	
	private String geraGrid(){
		
		//variaveis
		String vCodeLine             = null;
		List<Coligado> listaColigado = null;
		List<Empresa>  listaEmpresa  = null;
		List<String>   vColunas      = new ArrayList<String>();
		List<String>   vValores      = new ArrayList<String>();
		
		//Instanciando objetos
		ColigadoDao coligadoDao = new ColigadoDao();
		EmpresaDao  empresaDao  = new EmpresaDao();
		Empresa     empresa     = new Empresa();
		
		//Add valores ao arryList
		listaColigado = coligadoDao.consulta(null, null);

		//Montando Codigo
		vCodeLine  = "<ul id=\"iconbar\">";
		vCodeLine += " <li><a href=\"c02.jsp\">";
		vCodeLine += " <img src=\"sliding/sel.gif\" alt=\"\">";
		vCodeLine += " <span>Cadastrar Coligado</span></a>";
		vCodeLine += " </li>";
		vCodeLine += "</ul>";		
		vCodeLine += "<br><br>";
		vCodeLine += "<h2><font color=\"#F3F3F3\"><strong>Cadastro de Coligado</strong></font></h2>"; 
		vCodeLine += "<table width=\"75%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\" bordercolor=\"#CCCCCC\" bgcolor=\"#CCCCCC\">";
		vCodeLine += "<tr bgcolor=\"#708090\" align=\"center\">";
		vCodeLine += "<td height=\"50\" nowrap><h3><strong>&nbsp;&nbsp;Coligado&nbsp;</strong></h3></td>";
		vCodeLine += "<td height=\"50\" nowrap><h3><strong>&nbsp;&nbsp;Empresa&nbsp;</strong></h3></td>";
		vCodeLine += "<td height=\"30\" nowrap><h3><strong>&nbsp;&nbsp;</strong></h3></td>";
		vCodeLine += "<td height=\"30\" nowrap><h3><strong>&nbsp;&nbsp;</strong></h3></td>";
		vCodeLine += "</tr>";
		
		for (int i=0; i < listaColigado.size(); i++){
			
			Coligado coligado = listaColigado.get(i);
		
			if( i % 2 == 0){
				vCodeLine += "<tr align=\"center\" bgcolor=\"#F3F3F3\" bordercolor=\"#CCCCCC\">";
			}else{
				vCodeLine += "<tr align=\"center\" bgcolor=\"#E1E1E1\" bordercolor=\"#CCCCCC\">";
			}
			
			vCodeLine += "<td height=\"30\" nowrap><font size =\"2\"><b>&nbsp;"+coligado.getNOM_COLIGADA()+"&nbsp;</b></font></td>";
			
			vColunas.clear();
			vColunas.add("COD_EMPRESA");
			
			vValores.clear();
			vValores.add(coligado.getCOD_EMPRESA());
			
			listaEmpresa = empresaDao.consulta(vColunas, vValores);
			
			empresa = listaEmpresa.get(0);
			
			vCodeLine += "<td height=\"30\" nowrap><font size =\"2\"><b>&nbsp;"+empresa.getNOM_EMPRESA()+"&nbsp;</b></font></td>";
			
			vCodeLine += "<td height=\"30\" nowrap><font size =\"2\"><b>&nbsp;&nbsp;<a href=\"c03.jsp?id="+coligado.getCOD_COLIGADA()+"&id2="+coligado.getCOD_EMPRESA()+"\"><img src=\"resources/btn_alterar.png\"></a>&nbsp;&nbsp;</b></font></td>";
			vCodeLine += "<td height=\"30\" nowrap><font size =\"2\"><b>&nbsp;&nbsp;<a href=\"c04.jsp?id="+coligado.getCOD_COLIGADA()+"&id2="+coligado.getCOD_EMPRESA()+"\"><img src=\"resources/excluir.png\" name = \"excluir\"></a>&nbsp;&nbsp;</b></font></td>";
			vCodeLine += "</tr>";
		}
		vCodeLine +="</table>";
		
		return vCodeLine;
	}	
	
	public String getGrid(){
		return geraGrid();
	}	

	public String getAdd(){
		return geraCadastro();
	}
	
	public String getAlter(String pColigada, String pEmpresa){
		return geraAlterar(pColigada,pEmpresa);
	}
}
